﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using System.Web;
using System.Web.Http;
using BUS;
using DAO;

namespace AjaxChatService.Controllers
{
    [HttpHeader("Access-Control-Allow-Origin", "*")]
    public class UsersController : ApiController
    {
        private UserBUS userBUS = new UserBUS();
        // login 
        public User GetUser(string userName, string password)
        {
            User user = userBUS.LogUserIn(userName, password);
            return user;
        }

        public User GetUserByProviderInfo(string provider, string providerUserId)
        {
            User user = userBUS.LogUserInByProvider(provider, providerUserId);
            return user;
        }



        //GET api/Users/5
        public User GetUserById(int id)
        {
            User user = userBUS.GetUser(id);

            return user;
        }

        public HttpResponseMessage PostLogUserOut(int id)
        {
            userBUS.LogUserOut(id);
            return Request.CreateResponse(HttpStatusCode.OK);
        }

        // GET api/Users?userName=xxx
        public User GetUserByUserName(string userName)
        {
            User user = userBUS.GetUser(userName);
            if (user == null)
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
            }

            return user;
        }

        // PUT api/Users/5
        public HttpResponseMessage PutUser(int id, User user)
        {
            if (ModelState.IsValid && id == user.Id)
            {
                if (userBUS.UpdateUser(user))
                    return Request.CreateResponse(HttpStatusCode.OK);

                return Request.CreateResponse(HttpStatusCode.NotFound);
            }
            else
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }
        }

        // POST api/Users
        public HttpResponseMessage PostUser(User user)
        {
            if (ModelState.IsValid)
            {
                if(userBUS.AddUser(user))
                {
                    HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, user);
                    response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = user.Id }));
                    return response;  
                }

                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }
            else
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }
        }

        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
        }
    }
}